package com.rm.zeyou.util.uploadUtil;/**
 *
 */

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Description: 上传文件至Linux
 *
 * @author : YanYang
 */
@Slf4j
public class JSCHUploadFile {
    private static ChannelSftp sftp = null;

    private final static String username = "root";
    private final static String password = "www11223344.";
    private final static Integer port = 22;
    private final static String host = "106.14.123.255";
    private final static String savepath = "/home/ruoyi/uploadPath/";

    /**
     * Description: 向FTP服务器上传文件
     *
     * @param
     * @param
     * @return 成功返回true，否则返回false
     */
    public static String uploadFile(MultipartFile multipartFile) {

        FTPClient ftp = new FTPClient();
        try {
            JSch jsch = new JSch();
            //获取sshSession  账号-ip-端口

            Session sshSession = jsch.getSession(username, host, port);
            //添加密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //严格主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            //开启sshSession链接
            sshSession.connect();
            //获取sftp通道
            Channel channel = sshSession.openChannel("sftp");
            //开启
            channel.connect();
            sftp = (ChannelSftp) channel;

            //设置为被动模式
            ftp.enterLocalPassiveMode();
            //设置上传文件的类型为二进制类型
            //进入到要上传的目录  然后上传文件
            sftp.cd(savepath);
            sftp.put(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
            multipartFile.getInputStream().close();
            sftp.quit(); // 关闭SFTP通道
            // http://106.14.123.255/img/1.png
            return "http://106.14.123.255/img/" + multipartFile.getOriginalFilename();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return null;
    }


    public static String uploadFile(InputStream inputStream, String fileName) {

        FTPClient ftp = new FTPClient();
        try {
            JSch jsch = new JSch();
            //获取sshSession  账号-ip-端口

            Session sshSession = jsch.getSession(username, host, port);
            //添加密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //严格主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            //开启sshSession链接
            sshSession.connect();
            //获取sftp通道
            Channel channel = sshSession.openChannel("sftp");
            //开启
            channel.connect();
            sftp = (ChannelSftp) channel;

            //设置为被动模式
            ftp.enterLocalPassiveMode();
            //设置上传文件的类型为二进制类型
            //进入到要上传的目录  然后上传文件
            sftp.cd(savepath);
            sftp.put(inputStream, fileName);
            inputStream.close();
            sftp.quit(); // 关闭SFTP通道
            // http://106.14.123.255/img/1.png
            return "http://106.14.123.255/img/" + fileName;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    // 断开SSH会话
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return null;
    }

}
